/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import BE.Cliente;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author CarLita
 */
public class ClienteDao {
    Session session;

    public boolean guardar(Cliente c) {
        boolean ok = true;
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            session.saveOrUpdate(c);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();
            ok=false;
        } finally {
            session.close();
        }
        return ok;
    }

    public Cliente buscarPorId(int idCliente) {
            session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Cliente cliente where cliente.ruc = ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idCliente);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (Cliente)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

}
